/**
 * @file e4_t2_q5.c
 * @date 2022-03-09
 * 
 * @copyright Copyright (c) 2022 cnzyx MIT Licence
 * 
 */

#include<stdio.h>
#include<stdlib.h>

int halfSearch(int *array,int toFind,int len){
    int min=0,max=len-1,mid=0;
    while (min<=max){
        mid=(min+max)/2;
        if(array[mid]>toFind){
            max=mid-1;
        }else if(array[mid]<toFind){
            min=mid+1;
        }else{
            return mid;
        }
    }
    return -1;
}

int main(int argc, char const *argv[])
{
    //UTF-8
    system("chcp 65001");

    int array[100],len=0,in_num,result;
    printf("请输入数据，以空格隔开，以回车结束：\n");
    do{
        scanf("%d",&array[len]);
        len++;
    }while(getchar()!='\n');
    printf("请输入要查找的数据：");
    scanf("%d",&in_num);
    result=halfSearch(array,in_num,len);
    printf("结果：");
    if(result==-1){
        printf("未找到");
    }else{
        printf("下标为：%d",result);
    }
    printf("\n");

    //Pause console
    system("pause");
    return 0;
}
